package me.dengfengdecao.demo.service.impl;

import me.dengfengdecao.demo.dao.UserDao;
import me.dengfengdecao.demo.domain.User;
import me.dengfengdecao.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dengfengdecao on 16/9/26.
 */
@Service
@Transactional
@CacheConfig(cacheNames = {"user"})
public class UserServiceImpl implements UserService {

    public static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private UserDao userDao;

    @Cacheable
    @Override
    public User findUserById(Long id) {
        return userDao.findOne(id);
    }

    @Cacheable
    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @CacheEvict
    @Override
    public void delFromCache(Long id) {
        log.info("delFromCache: 移除缓存");
    }

    @Override
    public void save(User user) {
        userDao.save(user);
    }

    @Cacheable
    @Override
    public List<User> getUsers() {
        return (List<User>) userDao.findAll();
    }
}
